/**
 * 
 * @Project le cOeur - Data Virtualization
 * @Date 08.12.2012
 * @Author Aebi Pascal, Kocher Marc
 * @School BFH-TI Biel, Bachelor Thesis
 *
 */

package main.api.http;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * This class represents a component defined by the HTTP protocol (RFC-2616).
 */
public class MessageHeader implements Processable {

	private String messageHeader;
	private String fieldName;
	private String fieldValue;
	private List<MessageHeaderElement> headerElements;
	
	/**
	 * Creates a message header.
	 * The created message header needs to be parsed before the object can be used.
	 * 
	 * @param messageHeader The message header string containing a field name and a field value.
	 */
	public MessageHeader(String messageHeader) {
		this.headerElements = new ArrayList<>();
		this.messageHeader = messageHeader;
	}
	
	/**
	 * Creates a message header.
	 * 
	 * @param fieldName The field name.
	 * @param fieldValue The field value.
	 */
	public MessageHeader(String fieldName, String fieldValue) {
		this.headerElements = new ArrayList<>();
		this.fieldName = fieldName;
		this.fieldValue = fieldValue;
		this.parseFieldValue();
	}
	
	/**
	 * Creates a message header.
	 * 
	 * @param fieldName The field name.
	 * @param fieldValueElements The field value elements
	 */
	public MessageHeader(String fieldName, List<MessageHeaderElement> fieldValueElements) {
		this.fieldName = fieldName;
		this.headerElements = fieldValueElements;
		this.fieldValue = this.composeHeader();
	}
	
	@Override
	public void parse() {
		Pattern pattern = Pattern.compile("([\\w|-]+):\\s(.+)\\b");
		Matcher matcher = pattern.matcher(this.messageHeader);
		matcher.find();
		this.fieldName = matcher.group(1);
		this.fieldValue = matcher.group(2);
		String[] elements = this.fieldValue.split("\\s*,\\s*");
		for (String element : elements) {
			MessageHeaderElement messageHeaderelement = new MessageHeaderElement(element);
			messageHeaderelement.parse();
			this.headerElements.add(messageHeaderelement);
		}
	}
	
	@Override
	public String compose() {
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.setLength(0);
		stringBuilder.append(this.fieldName);
		stringBuilder.append(": ");
		if(this.headerElements != null){
			int elements = this.headerElements.size();
			for (int i = 0; i < elements; i++) {
				stringBuilder.append(this.headerElements.get(i).toString());
				if(i < elements-1){
					stringBuilder.append(", ");
				}
			}
		}
		stringBuilder.append("\r\n");
		return stringBuilder.toString();
	}
	
	@Override
	public String toString() {
		return this.compose();
	}
	
	/**
	 * Returns the field name of the message header.
	 * 
	 * @return The field name.
	 */
	public String getFieldName() {
		return this.fieldName;
	}
	
	/**
	 * Returns the field value of the message header.
	 * 
	 * @return The field value.
	 */
	public String getFieldValue() {
		return this.fieldValue;
	}
	
	/**
	 * Returns a list containing all field value elements of the message header.
	 * 
	 * @return The field value elements.
	 */
	public List<MessageHeaderElement> getFieldValueElements() {
		return this.headerElements;
	}
	
	/**
	 * Parses the field value of the message header.
	 * It creates the message header elements and adds them to the element list.
	 */
	private void parseFieldValue() {
		List<MessageHeaderElementParameter> messageHeaderElementParameters;
		String[] elements = this.fieldValue.split("\\s*,\\s*");
		String[] values;
		String[] parameters;
		String headerElementName = null;
		String headerElementValue = null;
		for (String element : elements) {
			MessageHeaderElement messageHeaderElement;
			messageHeaderElementParameters = new ArrayList<>();
			parameters = element.split("\\s*;\\s*");
			for (int i = 0; i < parameters.length; i++) {
				values = parameters[i].split("\\s*=\\s*");
				if(i == 0){
					headerElementName = values[0];
					if(values.length == 2){
						headerElementValue = values[1];
					}
				}
				else{
					messageHeaderElementParameters.add(new MessageHeaderElementParameter(values[0], values[1]));
				}
			}
			messageHeaderElement = new MessageHeaderElement(headerElementName, headerElementValue, messageHeaderElementParameters);
			this.headerElements.add(messageHeaderElement);
		}
	}
	
	/**
	 * Composes the field value of the message header from the element list.
	 * 
	 * @return The composed string.
	 */
	private String composeHeader() {
		StringBuilder stringBuilder = new StringBuilder();
		int elements = this.headerElements.size();
		for (int i = 0; i < elements; i++) {
			stringBuilder.append(this.headerElements.get(i).toString());
			if(i < elements-1){
				stringBuilder.append(", ");
			}
		}
		return stringBuilder.toString();
	}

}
